﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace nsGumController
{
    public class CLog
    {
        public CLog(RichTextBox richTextBox)
        {
            mRichTextBoxRemote = richTextBox;
        }

        public CLog(RichTextBox richTextBox, bool isLogToFile)
        {
            mRichTextBoxRemote = richTextBox;
            IsLogToFile = isLogToFile;
        }

        private RichTextBox mRichTextBoxRemote;
        private LogToFile mLogToFile = new LogToFile();
        public bool IsLogToFile = true;
        public delegate void LogAppendDelegate(Color color, string format, params object[] args);

        /// <summary>
        /// 追加显示文本
        /// </summary>
        /// <param name="color">文本颜色</param>
        /// <param name="text">显示文本</param>
        private void LogAppend(Color color, string format, params object[] args)
        {
            string text = "";
            try
            {
            	text = string.Format(format, args);
            }
            catch (Exception ex)
            {
                mRichTextBoxRemote.SelectionColor = Color.Red;
                mRichTextBoxRemote.AppendText(DateTime.Now.ToString("HH:mm:ss ")
                    + "格式化字符串有误：" + ex.Message + "\n");
                text = format;
            }
            text = DateTime.Now.ToString("HH:mm:ss ") + text;

            mRichTextBoxRemote.SelectionStart = mRichTextBoxRemote.TextLength;  // 插入点设到文本末尾
            mRichTextBoxRemote.SelectionColor = color;
            mRichTextBoxRemote.AppendText(text);
            mRichTextBoxRemote.ScrollToCaret();
            
            if (IsLogToFile)
            {
                try
                {
                    mLogToFile.LogInfo(text);
                }
                catch (Exception ex)
                {
                    mRichTextBoxRemote.SelectionColor = Color.Red;
                    mRichTextBoxRemote.AppendText(DateTime.Now.ToString("HH:mm:ss ")
                        + "写日志文件失败：" + ex.Message + "\n");
                }
            }
        }

        /// <summary>
        /// 显示错误日志
        /// </summary>
        /// <param name="text"></param>
        public void LogError(string format, params object[] args)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            mRichTextBoxRemote.Invoke(la, Color.Red, format, args);
        }

        /// <summary>
        /// 显示警告信息
        /// </summary>
        /// <param name="text"></param>
        public void LogWarning(string format, params object[] args)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            mRichTextBoxRemote.Invoke(la, Color.Orange, format, args);
        }

        /// <summary>
        /// 显示普通信息
        /// </summary>
        /// <param name="text"></param>
        public void LogMessage(string format, params object[] args)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            mRichTextBoxRemote.Invoke(la, Color.Black, format, args);
        }

        /// <summary>
        /// 显示调试信息
        /// </summary>
        /// <param name="text"></param>
        public void LogDebug(string format, params object[] args)
        {
#if DEBUG
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            mRichTextBoxRemote.Invoke(la, Color.Blue, format, args);
#endif
        }
    }

    /// <summary>
    /// 将日志信息写入到文件类
    ///  文件名为当前时间.txt
    /// </summary>
    public class LogToFile
    {
        string fileName;
        public LogToFile()
        {
            fileName = ".\\" + DateTime.Now.ToLocalTime().ToString("yyMMdd_hhmmss") + ".log";
        }

        public void LogInfo(string logStr)
        {
            StreamWriter sw = null;
            try
            {
                sw = new StreamWriter(fileName, true);
                sw.Write(logStr);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
            }
        }
    }
}
